//
//  CustonCollectionViewCell.m
//  WangI
//
//  Created by dllo on 15/11/23.
//  Copyright © 2015年 WJN. All rights reserved.
//

#import "CustonCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "CustomHeadTableViewCell.h"
#import "CustomThreeImageTableViewCell.h"
#import "CustomOneImageTableViewCell.h"
@implementation CustonCollectionViewCell

- (void)dealloc {
    [_tableView release];
    [_newHeaderModel release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        [self addSubview:self.tableView];
        [_tableView release];
    }
    return self;
}




#pragma mark -- UITableViewDelegate 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 
    return self.newHeaderModel.T1348647909107.count - 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
    NSArray *array =  self.newHeaderModel.T1348647909107[indexPath.row + 1][@"imgnewextra"];
    if (array.count != 0) {
        CustomThreeImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomThreeImageTableViewCellIdentifier"];
    
        NSMutableArray *arrData = [NSMutableArray arrayWithCapacity:0];
        [arrData addObject:self.newHeaderModel.T1348647909107[indexPath.row + 1][@"title"]];
        [arrData addObject:self.newHeaderModel.T1348647909107[indexPath.row + 1][@"imgsrc"]];
        [arrData addObject:array[0][@"imgsrc"]];
        [arrData addObject:array[1][@"imgsrc"]];
  
        cell.headerDataSource = arrData;
        return cell;
    } else {
        NSNumber *n = [NSNumber numberWithInteger:1];
        if ([self.newHeaderModel.T1348647909107[indexPath.row + 1][@"imgType"] isEqualToNumber:n]) {
            CustomOneImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomOneImageTableViewCellIdentifier"];
            NSMutableArray *arrayData = [NSMutableArray arrayWithCapacity:0];
            [arrayData addObject:self.newHeaderModel.T1348647909107[indexPath.row + 1][@"title"]];
            [arrayData addObject:self.newHeaderModel.T1348647909107[indexPath.row + 1][@"imgsrc"]];
            [arrayData addObject:self.newHeaderModel.T1348647909107[indexPath.row + 1][@"digest"]];
            cell.headerDataSource = arrayData;
            return cell;
        } else {
            CustomHeadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomHeaderTableViewCellIdentifier"];
            NSString *str1 = self.newHeaderModel.T1348647909107[indexPath.row + 1][@"imgsrc"];
            NSString *str2 = self.newHeaderModel.T1348647909107[indexPath.row + 1][@"title"];
            NSString *str3 = self.newHeaderModel.T1348647909107[indexPath.row + 1][@"digest"];
    
           NSArray *array = @[str1, str2, str3];
           cell.headerDataSource = array;
    
           return cell;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  NSArray *array =  self.newHeaderModel.T1348647909107[indexPath.row + 1][@"imgnewextra"];
    if (array.count != 0) {
        return 130;
    } else {
        NSNumber *n = [NSNumber numberWithInteger:1];
        if ([self.newHeaderModel.T1348647909107[indexPath.row + 1][@"imgType"] isEqualToNumber:n]) {
            return 170;
        } else {
            return 100;
        }
    }
}
#pragma mark -- setModel
-(void)setNewHeaderModel:(NewsHeaderModel *)newHeaderModel {
    if (_newHeaderModel != newHeaderModel) {
        [_newHeaderModel release];
        _newHeaderModel = [newHeaderModel retain];
  
    NSMutableArray *arrayImage = [NSMutableArray arrayWithCapacity:0];
    NSMutableArray *arrayTitle = [NSMutableArray arrayWithCapacity:0];
    NSArray *arr = newHeaderModel.T1348647909107[0][@"ads"];
    
    for (int i = 0; i < arr.count; i++) {
        NSString *str = arr[i][@"imgsrc"];
        NSString *str1 = arr[i][@"title"];
        [arrayImage addObject:str];
        [arrayTitle addObject:str1];
    }

    [arrayImage addObject:newHeaderModel.T1348647909107[0][@"imgsrc"]];
    [arrayTitle addObject:newHeaderModel.T1348647909107[0][@"title"]];

    PhotoClass *photo = [[PhotoClass alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 200) withArray:arrayImage withArrayTitle:arrayTitle];
 
    self.tableView.tableHeaderView = photo;
    [photo release];
    
        [self.tableView reloadData];
    }
}

#pragma mark -- layoutSubview

-(void)layoutSubviews  {
    [super layoutSubviews];
    _tableView.frame = self.bounds;
    
}

#pragma mark -- pravite

- (UITableView *)tableView {
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerClass:[CustomHeadTableViewCell class] forCellReuseIdentifier:@"CustomHeaderTableViewCellIdentifier"];
        [_tableView registerClass:[CustomThreeImageTableViewCell class] forCellReuseIdentifier:@"CustomThreeImageTableViewCellIdentifier"];
        [_tableView registerClass:[CustomOneImageTableViewCell class] forCellReuseIdentifier:@"CustomOneImageTableViewCellIdentifier"];
        
        [self addheader];
        _tableView.headerRefreshingText = @"正在刷新";
        _tableView.headerReleaseToRefreshText = @"松开刷新";
        
        [_tableView headerBeginRefreshing];
        
        [self addfooter];
        _tableView.footerRefreshingText = @"正在加载";
        _tableView.footerReleaseToRefreshText = @"松开加载";
        
    }
    return _tableView;
}


#pragma mark -- relodata

- (void)addheader {
    [_tableView addHeaderWithCallback:^{

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            [_tableView reloadData];
            [_tableView headerEndRefreshing];
        });
    }];
}


- (void)addfooter {
    [_tableView addFooterWithCallback:^{

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [_tableView reloadData];
            [_tableView footerEndRefreshing];
        });
    }];
}



@end
